package com.gin.no_gay_alliance.activity;

import android.annotation.SuppressLint;
import android.app.DownloadManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;

import com.gin.jackson.utils.JacksonUtils;
import com.gin.jackson.utils.ObjectUtils;
import com.gin.no_gay_alliance.R;
import com.gin.no_gay_alliance.adpter.AttachmentItemAdapter;
import com.gin.no_gay_alliance.databinding.ActivityAttachmentBinding;
import com.gin.no_gay_alliance.exception.RequireParamException;
import com.gin.no_gay_alliance.utils.AlertDialogUtils;
import com.gin.no_gay_alliance.viewmodel.AttachmentModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

/**
 * 附件列表活动
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/3 15:17
 **/
public class AttachmentListActivity extends BaseBindingActivity<ActivityAttachmentBinding> {
    public static final String TAG = AttachmentListActivity.class.getSimpleName();
    private static final String ARG_KEY = "attachment";
    private ArrayList<AttachmentModel> data;
    private AttachmentItemAdapter adapter;

    @Override
    protected void handleIntent(Intent intent) {
        data = intent.getParcelableArrayListExtra(ARG_KEY);
        JacksonUtils.printPretty(data);
        if (ObjectUtils.isEmpty(data)) {
            throw new RequireParamException();
        }
    }

    @Override
    protected void onCreate() {
        adapter = new AttachmentItemAdapter(this, data);
        b.recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
        menuInflater.inflate(R.menu.attachment_list_menu, menu);
    }

    @Override
    protected Toolbar initToolbar() {
        return b.toolbar;
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem menuItem) {
        // 下载选中附件
        if (menuItem.getItemId() == R.id.menu_item_save) {
            List<AttachmentModel> downloadList = data.stream().filter(i -> i.checked).collect(Collectors.toList());
            if (downloadList.isEmpty()) {
                return true;
            }
            // 附件总大小
            long sumSize = downloadList.stream().mapToLong(i -> i.size).sum();
            String message = String.format(Locale.CHINA, "即将保存选中的所有附件, 总大小 %.1f MB", sumSize / 1024f);

            AlertDialogUtils.showConfirm(this, "保存选中附件", message, (dialog, which) -> {
                if (which == DialogInterface.BUTTON_POSITIVE) {
                    downloadWithSystem(downloadList);
                }
            });
            return true;
        }
        // 全选
        if (menuItem.getItemId() == R.id.menu_item_check_all) {
            data.forEach(i -> i.checked = true);
            adapter.notifyDataSetChanged();
        }
        // 全不选
        if (menuItem.getItemId() == R.id.menu_item_uncheck_all) {
            data.forEach(i -> i.checked = false);
            adapter.notifyDataSetChanged();
        }

        return super.onOptionsItemSelected(menuItem);
    }


    /**
     * 使用系统下载器下载所有附件
     */
    private void downloadWithSystem(List<AttachmentModel> downloadList) {
        DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        downloadList.stream().map(AttachmentModel::buildDownloadRequest).forEach(downloadManager::enqueue);
        toast(String.format(Locale.CHINA, "开始下载 %d 个文件", downloadList.size()));
    }

    public static void startActivity(Context context, ArrayList<AttachmentModel> attachments) {
        Intent intent = new Intent(context, AttachmentListActivity.class);
        intent.putParcelableArrayListExtra(ARG_KEY, attachments);
        context.startActivity(intent);
    }
}
